﻿CREATE TABLE [CD].[InventoryLines] (
    [Id]             INT           NOT NULL,
    [DepartmentId]   AS            ([Id]/(100000000)),
    [DocId]          INT           NOT NULL,
    [PartyId]        INT           NULL,
    [GoodsDocLineId] INT           NULL,
    [DocQntty]       INT           CONSTRAINT [DF_InventoryLines_DocQntty] DEFAULT ((1)) NOT NULL,
    [FactQntty]      INT           CONSTRAINT [DF_InventoryLines_FactQntty] DEFAULT ((1)) NOT NULL,
    [Note]           VARCHAR (250) NULL,
    [MD]             DATETIME      CONSTRAINT [DF__InventoryLin__MD__149C0161] DEFAULT (getdate()) NOT NULL,
    [MW]             VARCHAR (50)  CONSTRAINT [DF__InventoryLin__MW__1590259A] DEFAULT (suser_sname()) NOT NULL,
    CONSTRAINT [PK_InventoryLines] PRIMARY KEY NONCLUSTERED ([Id] ASC),
    CONSTRAINT [FK_InventoryLines_Documents] FOREIGN KEY ([DocId]) REFERENCES [CA].[Documents] ([Id]),
    CONSTRAINT [FK_InventoryLines_GoodsDocLines] FOREIGN KEY ([GoodsDocLineId]) REFERENCES [CA].[GoodsDocLines] ([Id]),
    CONSTRAINT [FK_InventoryLines_Parties] FOREIGN KEY ([PartyId]) REFERENCES [CD].[Parties] ([Id])
);


GO
CREATE NONCLUSTERED INDEX [IX_InventoryLines_DepartmentId]
    ON [CD].[InventoryLines]([DepartmentId] ASC);


GO
CREATE NONCLUSTERED INDEX [IX_InventoryLines_MD]
    ON [CD].[InventoryLines]([MD] ASC);


GO

CREATE TRIGGER CD.InventoryLinesDeleteLog ON CD.InventoryLines AFTER DELETE
AS 
BEGIN
	SET NOCOUNT ON;

INSERT INTO [PR].[DocDeleteLog] (TableName, IdValue, MD, MW)
SELECT 'CD.InventoryLines', Id, getdate(), SYSTEM_USER FROM DELETED

END
